import java.util.Arrays;

public class Solution566 {
    public int[][] matrixReshape(int[][] mat, int r, int c) {
        if (r*c!=mat.length*mat[0].length){
            return mat;
        }
        int[][] f=new int[r][c];
        for (int i=0;i<r*c;i++){
            f[i/c][i%c]=mat[i/mat[0].length][i%mat[0].length];
        }
        return f;
    }

    public static void main(String[] args) {
        int[][] f=new Solution566().matrixReshape(new int[][]{{1,2},{3,4}},1,4);
        for (int[] ints : f) {
            System.out.println(Arrays.toString(ints));
        }
    }
}
